#include <OneWire.h>
#include <DallasTemperature.h>
#define LONE_WIRE_BUS 2
#define RONE_WIRE_BUS 3
OneWire LoneWire(LONE_WIRE_BUS);
OneWire RoneWire(RONE_WIRE_BUS);
DallasTemperature Lsensors(&LoneWire);
DallasTemperature Rsensors(&RoneWire);
#define Lrelay 4
#define Rrelay 5
float Lt, Rt;
String s = "", s1 = "";
int LDT = 10 , RDT = 10;
bool flag = 0;

void setup() {
  Serial.begin(9600);
  Serial3.begin(9600);
  Serial1.begin(115200);
  Serial2.begin(115200);
  Lsensors.begin();
  Rsensors.begin();
  pinMode(Lrelay, OUTPUT);
  pinMode(Rrelay, OUTPUT);
  digitalWrite(Lrelay, HIGH);
  digitalWrite(Rrelay, HIGH);
  while (1) {
    while (Serial1.available() > 0) {
      s =  s + char(Serial1.read());
    }
    while (Serial2.available() > 0) {
      s1 =  s1 + char(Serial2.read());
    }
    s = s + "*" + s1;
    Serial3.print(s);
    //    Serial.print(s);
    s1 = "";  s = "";
    delay(2000);
    if (Serial3.available()) {
      int command = Serial3.read();
      //      Serial.println(command);
      if (command == 0xf1) {
        break;
      }
    }
  }
}

void loop() {
  if (flag) {
    (LDT < Lt) ? digitalWrite(Lrelay, LOW) : digitalWrite(Lrelay, HIGH);
    (RDT < Rt) ? digitalWrite(Rrelay, LOW) : digitalWrite(Rrelay, HIGH);
  }
  else {
    digitalWrite(Lrelay, HIGH);
    digitalWrite(Rrelay, HIGH);
  }
  if (Serial3.available()) {
    int command = Serial3.read();
    switch (command) {
      case 0xf2:
        LDT++;
        break;
      case 0xf3:
        LDT--;
        break;
      case 0xf4:
        flag = !flag;
        break;
      case 0xf5:
        RDT++;
        break;
      case 0xf6:
        RDT--;
        break;
      case 0xf7:
        Lsensors.requestTemperatures();
        Lt = Lsensors.getTempCByIndex(0);
        Rsensors.requestTemperatures();
        Rt = Rsensors.getTempCByIndex(0);
        String s = "A" + String(LDT) + "," + String(RDT) + "," + String(Lt) + "," + String(Rt) + "," + String(flag) + ",Z";
        Serial3.print(s);
        //        Serial.println(s);
        break;
      default:
        break;
    }
  }
}
